package com.vdongchina.thread.base.demo.databaseconnectionpool;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 易永耀
 */
public class ConnectionPoolTest {

    static ConnectionPool pool = new ConnectionPool(10);
    /**
     * 保证所有connectionRunner能够同时开始
     */
    static CountDownLatch start = new CountDownLatch(1);
    /**
     * main线程将会等待所有 connectionRunner结束才能继续执行
     */
    static CountDownLatch end;

    public static void main(String[] args) throws Exception {

//        int thredCount = 10;   线程数
//        int thredCount = 20;
//        int thredCount = 30;
        int thredCount = 20;
        end = new CountDownLatch(thredCount);
        int count = 20;
        AtomicInteger got = new AtomicInteger();
        AtomicInteger notGot = new AtomicInteger();
        for (int i = 0; i < thredCount; i++) {
            Thread thread = new Thread(new ConnectionRunner(count, got, notGot), "connectionRunnerThread" + i);
            thread.start();
        }
//        try {
//            TimeUnit.MILLISECONDS.sleep(100);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println("主线程初始化所有子线程, 准备并发执行!");
        start.countDown();
        end.await();
        System.out.println("total invoke: " + (thredCount * count));
        System.out.println("got connection: " + got);
        System.out.println("not got connection: " + notGot);

    }


    private static class ConnectionRunner implements Runnable {
        int count;
        AtomicInteger got;
        AtomicInteger notGot;

        public ConnectionRunner(int count, AtomicInteger got, AtomicInteger notGot) {
            this.count = count;
            this.got = got;
            this.notGot = notGot;
        }

        @Override
        public void run() {

            try {
                System.out.println("线程被主线程等待 : " + Thread.currentThread().getName());
                start.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程开始执行: " + Thread.currentThread().getName());
            while (count > 0) {
                try {
                    Connection connection = pool.fetchConnection(1000);
                    if (connection != null) {
                        try {
                            connection.createStatement();
                            connection.commit();
                        } finally {
                            pool.releaseConnection(connection);
                            got.incrementAndGet();
                        }
                    } else {
                        notGot.incrementAndGet();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    count--;
                }
            }
            end.countDown();
        }
    }
}
